<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            扫码绑定
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">发货单号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="扫描/输入 发货单号" id="consignment" value="" onclick="getIdSelector();"
                            onfocus="focusFn(this)">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">托号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="扫描/输入 托号" id="trayyinfoid" value="" onclick="getIdSelector();"
                            onfocus="focusFn(this)">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">箱号</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="扫描箱号" id="boxnum" value="" onclick="getIdSelector();"
                            onfocus="focusFn(this)">
                    </div>
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">条码</div>
                    <input type="text" class="aui-input" id="tm">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">物料号</div>
                    <input type="text" class="aui-input" id="mrlCode" disabled="true">
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">数量</div>
                    <input type="text" class="aui-input" id="qty" disabled="true">
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">批次号</div>
                    <input type="text" class="aui-input" disabled="true" id="lotCode">
                </div>
            </li>
            <li class="aui-list-item" style="display:none">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">条码ID</div>
                    <div class="aui-list-item-input">
                        <input type="text" id="tmID">
                    </div>
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <!-- <div class="aui-btn aui-btn-info" style="margin-left:5%;width:20%" onclick="search()">查询</div> -->
        <div class="aui-btn aui-btn-info" style="margin-left:5%;width:40%;" onclick="goPage()">按箱撤回</div>
        <div class="aui-btn aui-btn-info" style="margin-left:10%;width:40%;" onclick="deleteData()">批量撤回</div>
        <!-- <div class="aui-btn aui-btn-info" style="margin-left:2%;width:20%" onclick="save()">绑定</div> -->

        <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;margin-bottom:50px;">
            <table class="table" style='width:100%;' id="table">
                <thead>
                    <tr
                        style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 12px;font-weight: bold;">
                        <!-- <td></td> -->
                        <!-- <td algin='center'>序号</td> -->
                        <td algin='center'></td>
                        <td algin='center'>物料号</td>
                        <td algin='center'>物料名</td>
                        <td algin='center'>要货数量</td>
                        <td algin='center'>绑定数量</td>
                        <td style="display:none;">id</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtml">
                </tbody>
            </table>
        </div>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var checkFlag; //菜单路由参数
    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();


    }

    //初始化
    apiready = function () {
        //菜单参数
        var query = api.pageParam.query;
        checkFlag = query.checkFlag;

        $("#consignment").focus();
        idSelector = "consignment";
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            //1：看板
            if (idSelector == 'consignment') {
                
                $("#consignment").blur();
                //发货单号赋值
                $("#consignment").val(ret);

                //查询看板明细数据
                search();

                //2:库位
            } else if (idSelector == 'boxnum') {
                $("#boxnum").blur();

                //条码解析+自栋绑定
                tmSplit(ret);
            } else if (idSelector == 'trayyinfoid') {
                $("#trayyinfoid").blur();
                //发货单号赋值
                $("#trayyinfoid").val(ret);
                bind();
            }
        });
    }

    //箱号监听回车事件
    $("#consignment").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var consignment = $(this).val();
        if (keycode == '13') 
        {
            if(consignment != null && consignment != "" && consignment != undefined){
                $("#consignment").blur();
            console.log(consignment);
            $("#consignment").val(consignment);
            //$("#consignment").select();
            search();
            }
            
        }
    });
    //托码监听回车事件
    $("#trayyinfoid").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var trayyinfoid = $(this).val();
        if (keycode == '13') {
            if(trayyinfoid != null && trayyinfoid != "" && trayyinfoid != undefined){
                $("#trayyinfoid").blur();
            console.log(trayyinfoid);
            
            $("#trayyinfoid").val(trayyinfoid);
            bind();
            }
            
        }
    });

    //条码解析
    function tmSplit(tm) {
        var arr = tm.split(",");
        //物料号
        $("#mrlCode").val(arr[1]);
        //数量
        $("#qty").val(arr[2]);
        $("#lotCode").val(arr[3]);
        $("#tmID").val(arr[4]);
        //箱号
        $("#boxnum").val(arr[9]);
        //条码
        $("#tm").val(tm);

        //箱号全选
        // $("#boxnum").select();

        //校验箱号是否入库
        boxnumIn()
    }

    function boxnumIn() {
        checkFlag
        if (checkFlag == null || checkFlag == "" || checkFlag == undefined) {
            api.toast({
                msg: '请在菜单配置是否校验箱入库标识！',
                duration: 3000
            });
            return;
        }
        if (checkFlag == 'Y') {
            //箱号
            var boxnum = $("#boxnum").val();

            api.showProgress({
                title: '箱号校验中...',
            });

            var un = localStorage.getItem('un');
            var serverUrl = localStorage.getItem('serverUrl');
            var url = serverUrl + "/app/boxnumIn";
            var data = {
                values: {
                    boxnum: boxnum, //箱号
                    un: un
                }
            };
            api.ajax({
                url: url,
                method: 'post',
                dataType: 'json',
                timeout: 300,
                returnAll: false,
                data: data
            }, function (ret, err) {
                api.hideProgress();
                if (ret) {
                    var data = JSON.stringify(ret);
                    var myobj = eval("(" + data + ")");
                    //成功
                    if (myobj.code == '0') {
                        //自动绑定
                        save();
                        //失败
                    } else if (myobj.code == '1') {

                        //$("#boxnum").val("");
                        $("#boxnum").val("");
                    $("#boxnum").select();
                    idSelector="boxnum";
                        api.toast({
                            msg: myobj.message,
                            duration: 5000
                        });

                        //$("#boxnum").focus();
                        //$("#boxnum").select();
                    }
                }
            });
        } else {
            //自动绑定
            save();
        }

    }


    //箱号监听回车事件
    $("#boxnum").on('keypress', function (e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if(tm != null && tm != "" && tm != undefined){
                $("#boxnum").blur();
            console.log(tm);
            //条码解析+保存
            tmSplit(tm);
            }
           
        }
    });

    //查询看板明细数据
    function search(inputType) {
        var consignment = $("#consignment").val();
        if (consignment == "" || consignment == null || consignment == undefined) {
            api.toast({
                msg: '发货单号不能为空',
                duration: 5000
            });
            return;
        }
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getKanbanDetails";
        var data = {
            values: {
                consignment: consignment,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {
                    var list = myobj.list;
                    var html = "";
                    for (var i = 0; i < list.length; i++) {
                        html += "<tr style='text-align: center;background-color: #F5F5F5;color: black;font-size: 11px;'>";
                            html += "<td style='width:40px;'><input name='sub' type='checkbox' /></td>";
                        //html += "<td><input name='sub' type='radio' /></td>";
                        // html += "<td name='seqNo'>" + list[i].seqNo + "</td>";
                        html += "<td name='sitm_code'>" + list[i].sitm_code + "</td>";
                        html += "<td name='sitm_name'>" + list[i].sitm_name + "</td>";
                        html += "<td name='need_qty'>" + list[i].need_qty + "</td>";
                        html += "<td name='autualqty'>" + list[i].autualqty + "</td>";
                        html += "<td name='sysid' style='display:none;'>" + list[i].sysid + "</td>";
                        html += "</tr>";
                    }
                    $api.html($api.byId("tbodyHtml"), html);
                    if(inputType == "trayyinfoid"){
                        $("#trayyinfoid").focus();
                        idSelector = "trayyinfoid";

                    }else{
                        $("#boxnum").focus();
                        idSelector = "boxnum";
                    }
                    

                    //失败，存在没有发货单数据
                } else if (myobj.code == "1") {
                    $("#consignment").val("");
                    $("#consignment").select();
                    idSelector="consignment";

                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 5000
                    });
                   // $("#consignment").focus();
                   // $("#consignment").select();
                }
            }else{
                $("#consignment").val("");
                $("#consignment").select();
                 idSelector="consignment";
                 api.hideProgress();
                 //alert(JSON.stringify(err));
                 alert("连接超时，请耐心等待");
            }

        });
    }


    //批量撤回
    function deleteData() {
        //发货单号
        var consignment = $("#consignment").val();
        //发货单号
        if (consignment == "" || consignment == null || consignment == undefined) {
            api.toast({
                msg: '请扫描/输入发货单号',
                duration: 5000
            });
            return;
        }
        //选中数据
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        check.each(function () {
            count++;
        });
        //选中行不为1
        if (count == 0) {
            api.toast({
                msg: '请选择至少一条数据！',
                duration: 3000
            });
            return;
        }
        var list = [];

        check.each(function () { //遍历
            var row = $(this).parent("td").parent("tr");
            var sysid = row.find("[name='sysid']").html();

            list.push({
                "sysid": sysid,
            });
        });

        api.showProgress({
            title: '删除中...',
        });

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/uwmSFKDelete";
        var params = new Object();
        var data = {
            values: {
                consignment: consignment, //发货单号
                list: JSON.stringify(list),
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();
                    api.toast({
                        msg: '删除成功',
                        duration: 5000
                    });
                    $("#boxnum").val("");
                    $("#tm").val("");
                    $("#mrlCode").val("");
                    $("#qty").val("");
                    search();

                    //失败
                } else if (myobj.code == '1') {

                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 5000
                    });
                }
            }
        });
    }

    //保存按钮
    function save() {
        //发货单号
        var consignment = $("#consignment").val();
        //箱号
        var boxnum = $("#boxnum").val();
        //条码解析+保存
        //  tmSplit(boxnum);
        //数量
        var qty = $("#qty").val();
        //条码
        var tm = $("#tm").val();
        //发货单号
        if (consignment == "" || consignment == null || consignment == undefined) {
            api.toast({
                msg: '请扫描/输入发货单号',
                duration: 5000
            });
            return;
        }

        //箱号
        if (boxnum == "" || boxnum == null || boxnum == undefined) {
            api.toast({
                msg: '请扫描箱号',
                duration: 5000
            });
            return;
        }

        //箱号数量必须大于0
        if (qty == "" || qty == null || qty == undefined || parseInt(qty) == 0) {
            api.toast({
                msg: '箱号解析数量必须大于0',
                duration: 5000
            });
            return;
        }

        //条码特殊字符处理
        //       tm = encodeURIComponent(encodeURIComponent(tm));

        api.showProgress({
            title: '绑定中...',
        });

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/uwmSFKScanCodeBinding";
        var data = {
            values: {
                consignment: consignment, //发货单号
                tm: tm, //条码
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    api.hideProgress();
                    api.toast({
                        msg: '绑定成功',
                        duration: 5000
                    });

                    //清空表单
                    $("#boxnum").val("");
                    $("#tm").val("");
                    $("#mrlCode").val("");
                    $("#qty").val("");
                    search();

                    //失败
                } else if (myobj.code == '1') {
                    $("#boxnum").val("");
                    $("#boxnum").select();
                    idSelector="boxnum";

                    api.hideProgress();
                   // $("#boxnum").val("");
                    api.toast({
                        msg: myobj.message,
                        duration: 5000
                    });
                    //$("#boxnum").focus();
                }
            }else{
                $("#boxnum").val("");
                $("#boxnum").select();
                 idSelector="boxnum";
                 api.hideProgress();
                // alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });
    }


    //托号绑定
    function bind() {
        //发货单号
        var consignment = $("#consignment").val();
        //托码
        var trayyinfoid = $("#trayyinfoid").val();

        //发货单号
        if (consignment == "" || consignment == null || consignment == undefined) {
            api.toast({
                msg: '请扫描/输入发货单号',
                duration: 5000
            });
            return;
        }
        //托码
        if (trayyinfoid == "" || trayyinfoid == null || trayyinfoid == undefined) {
            api.toast({
                msg: '请扫描托码！',
                duration: 5000
            });
            return;
        }


        api.showProgress({
            title: '绑定中...',
        });

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/uwmTrayBinding";
        var data = {
            values: {
                consignment: consignment, //发货单号
                trayCode: trayyinfoid, //托号
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();
                    api.toast({
                        msg: '绑定成功',
                        duration: 5000
                    });

                    //清空表单
                    $("#trayyinfoid").val("");
                    $("#tm").val("");
                    $("#mrlCode").val("");
                    $("#qty").val("");
                    search("trayyinfoid");
                    $("#trayyinfoid").val("");
                    $("#trayyinfoid").select();
                    idSelector="trayyinfoid";
                    //失败
                } else if (myobj.code == '1') {
                    $("#trayyinfoid").val("");
                    $("#trayyinfoid").select();
                    idSelector="trayyinfoid";

                    api.hideProgress();
                   // $("#trayyinfoid").val("");
                    api.toast({
                        msg: myobj.message,
                        duration: 5000
                    });
                   // $("#trayyinfoid").focus();
                }
            }else{
                $("#trayyinfoid").val("");
                $("#trayyinfoid").select();
                 idSelector="trayyinfoid";
                 api.hideProgress();
                 //alert(JSON.stringify(err));
                 alert("连接超时，请耐心等待");
            }

        });
    }
    // //托号绑定
    // function bind() {
    //     var trayyinfoid = $("#trayyinfoid").val();
    //     var tm = $("#tm").val();
    //     var supplierCode = "";
    //     var mrlCode = $("#mrlCode").val();
    //     var lotCode = $("#lotCode").val();
    //     var qty = $("#qty").val();
    //     var boxnum = $("#boxnum").val();
    //     //条码ID
    //     var tmID = $("#tmID").val();

    //     //托码
    //     if (trayyinfoid == "" || trayyinfoid == null || trayyinfoid == undefined) {
    //         api.toast({
    //             msg: '请扫描托码！',
    //             duration: 5000
    //         });
    //         return;
    //     }

    //     //条码
    //     if (tm == "" || tm == null || tm == undefined) {
    //         api.toast({
    //             msg: '请扫描条码！',
    //             duration: 5000
    //         });
    //         return;
    //     }
    //     //物料号非空校验
    //     if (mrlCode == "" || mrlCode == null || mrlCode == undefined) {
    //         api.toast({
    //             msg: '物料号不能为空！',
    //             duration: 5000
    //         });
    //         return;
    //     }
    //     //批次号非空校验
    //     if (qty == "" || qty == null || qty == undefined || parseFloat(qty) <= 0) {
    //         api.toast({
    //             msg: '数量必须大于0！',
    //             duration: 5000
    //         });
    //         return;
    //     }

    //     //箱号非空校验
    //     if (boxnum == "" || boxnum == null || boxnum == undefined) {
    //         api.toast({
    //             msg: '箱号不能为空！',
    //             duration: 5000
    //         });
    //         return;
    //     }

    //     var un = localStorage.getItem("un");
    //     var serverUrl = localStorage.getItem('serverUrl');
    //     var url = serverUrl + "/app/wmTrayinfoBindNew";
    //     var data = {
    //         values: {
    //             consignment: consignment, //发货单号
    //             trayyinfoid: trayyinfoid, //托码
    //             tm: tm, //条码
    //             mrlCode: mrlCode, //物料号
    //             lotCode: lotCode, //批次号
    //             qty: qty, //数量
    //             boxnum: boxnum, //箱号
    //             tmID: tmID,
    //             checkFlag: "Y",//是否需要校验箱是否入库 Y-是 其他则不校验
    //             un: un
    //         }
    //     };
    //     api.ajax({
    //         url: url,
    //         method: 'post',
    //         dataType: 'json',
    //         timeout: 300,
    //         returnAll: false,
    //         data: data
    //     }, function(ret, err) {
    //         if (ret) {
    //             var data = JSON.stringify(ret);
    //             var myobj = eval("(" + data + ")");
    //             if (myobj.code == '0') {

    //                 api.toast({
    //                     msg: "绑定成功",
    //                     duration: 5000
    //                 });

    //                 //清空表单数据
    //                 $("#tm").val("");
    //                 $("#supplierCode").val("");
    //                 $("#mrlCode").val("");
    //                 $("#lotCode").val("");
    //                 $("#qty").val("");
    //                 $("#boxnum").val("");
    //                 //光标回到条码
    //                 $("#tm").focus();

    //             } else {

    //                 $("#tm").val("");
    //                 api.toast({
    //                     msg: myobj.message,
    //                     duration: 5000
    //                 });
    //             }
    //         } else {

    //             $("#tm").val("");
    //             alert(JSON.stringify(err));
    //         }
    //     });
    // }

    //删除选中绑定
    function deleteDataOne() {
        var check = $("input[type='radio']:checked"); //在table中找input下类型为radio属性为选中状态的数据
        var count = 0;
        check.each(function () {
            count++;
        });
        var id = "";

        //选中行不为1
        if (count == 0) {
            api.toast({
                msg: '请选择一条数据！',
                duration: 3000
            });
            return;
        }

        check.each(function () { //遍历
            var row = $(this).parent("td").parent("tr");
            id = row.find("[name='sysid']").html();
        });
        //发货单号
        var consignment = $("#consignment").val();
        //发货单号
        if (consignment == "" || consignment == null || consignment == undefined) {
            api.toast({
                msg: '请扫描/输入发货单号',
                duration: 5000
            });
            return;
        }

        api.showProgress({
            title: '删除中...',
        });

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/uwmSFKDeleteOne";
        var params = new Object();
        var data = {
            values: {
                consignment: consignment, //发货单号
                un: un,
                id: id
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();
                    api.toast({
                        msg: '删除成功',
                        duration: 5000
                    });
                    $("#boxnum").val("");
                    $("#tm").val("");
                    $("#mrlCode").val("");
                    $("#qty").val("");
                    search();

                    //失败
                } else if (myobj.code == '1') {

                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 5000
                    });
                }
            }
        });
    }

    //订单返回按钮
    function closeWin() {
        api.closeWin({});
    }
    function focusFn(ele) {
        $api.attr(ele, 'readonly', 'readonly');
        setTimeout(() => {
            $api.removeAttr(ele, 'readonly');
        }, 200);
    };
    function goPage() {
        var consignment = $("#consignment").val();
        //打开页面
        api.openWin({
            name: 'deletePackge',
            url: 'deletePackge.html',
            pageParam: {
                consignment: consignment,
            }
        });
    }

</script>

</html>